/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controleur;

import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.Map;
import modele.Article;
import modele.NewHibernateUtil;
import modele.Personne;
import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.Session;

/**
 *
 * @author Aimee
 */
public class AjoutArticle extends ActionSupport implements SessionAware {

    public Map<String, Object> session;
    public Session hibernateSession;
    public String idPersonne;
    public String contenuArticle;
    public String titreArticle;
    public List<Article> listeArticle;

    public String ajoutArticle() throws Exception {
        setHibernateSession(NewHibernateUtil.getSessionFactory().openSession());
        getHibernateSession().beginTransaction();

        Article article = new Article();
        Personne pers = (Personne) hibernateSession.createQuery("from Personne where idPersonne =" + idPersonne).uniqueResult();
        article.setPersonne(pers);
        article.setTitreArticle(titreArticle);
        article.setContenuArticle(contenuArticle);
        getHibernateSession().save(article);
        getHibernateSession().getTransaction().commit();

        listeArticle = hibernateSession.createQuery("from Article where idPersonne= " + idPersonne).list();
        session.put("listeArticle", listeArticle);
        return "ok";
    }

    /**
     * @return the session
     */
    public Map<String, Object> getSession() {
        return session;
    }

    /**
     * @param session the session to set
     */
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    /**
     * @return the hibernateSession
     */
    public Session getHibernateSession() {
        return hibernateSession;
    }

    /**
     * @param hibernateSession the hibernateSession to set
     */
    public void setHibernateSession(Session hibernateSession) {
        this.hibernateSession = hibernateSession;
    }

    /**
     * @return the idPersonne
     */
    public String getIdPersonne() {
        return idPersonne;
    }

    /**
     * @param idPersonne the idPersonne to set
     */
    public void setIdPersonne(String idPersonne) {
        this.idPersonne = idPersonne;
    }

    /**
     * @return the contenuArticle
     */
    public String getContenuArticle() {
        return contenuArticle;
    }

    /**
     * @param contenuArticle the contenuArticle to set
     */
    public void setContenuArticle(String contenuArticle) {
        this.contenuArticle = contenuArticle;
    }

    /**
     * @return the titreArticle
     */
    public String getTitreArticle() {
        return titreArticle;
    }

    /**
     * @param titreArticle the titreArticle to set
     */
    public void setTitreArticle(String titreArticle) {
        this.titreArticle = titreArticle;
    }

    /**
     * @return the listeArticle
     */
    public List<Article> getListeArticle() {
        return listeArticle;
    }

    /**
     * @param listeArticle the listeArticle to set
     */
    public void setListeArticle(List<Article> listeArticle) {
        this.listeArticle = listeArticle;
    }
}
